﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Rung.EBook.WebSiteAnalizer;
using Rung.EBook.WebSiteAnalizer.Driver;

namespace Rung.EBook.UI.Forms
{
    public partial class LoadAnalizerAddForm : Form
    {
        private LoadAnalizerForm parent;

        public LoadAnalizerAddForm()
        {
            InitializeComponent();
        }

        public LoadAnalizerAddForm(LoadAnalizerForm parent)
            : this()
        {
            this.parent = parent;
        }

        // 将 parent.mAnalizerForUse 反映到本窗体上。
        private void AnalizerForUseToUI()
        {
            if (null == parent.mAnalizerForUse) return;

            txbName.Text = parent.mAnalizerForUse.Name;
            txbSiteName.Text = parent.mAnalizerForUse.SiteName;
            txbSiteUrl.Text = parent.mAnalizerForUse.SiteUrl;
            ddlMethod.SelectedIndex = (parent.mAnalizerForUse.Method == HttpMethod.Get) ? 0 : 1;
            ddlEcoding.SelectedItem = parent.mAnalizerForUse.Encoding == Encoding.UTF8 ? "utf-8" : "gb2312";

            txbBookNamePrefix.Text = parent.mAnalizerForUse.BookNamePrefix;
            txbBooksUrl.Text = parent.mAnalizerForUse.BooksUrl;
            txbBookRegex.Text = parent.mAnalizerForUse.BookRegex;

            txbChaptersUrl.Text = parent.mAnalizerForUse.ChaptersUrl;
            txbChapterRegex.Text = parent.mAnalizerForUse.ChapterRegex;

            txbContentBaseUrl.Text = parent.mAnalizerForUse.ContentBaseUrl;
            txbContentRegex.Text = parent.mAnalizerForUse.ContentRegex;

            if (null != parent.mAnalizerForUse.ContentClears)
            {
                foreach (var c in parent.mAnalizerForUse.ContentClears)
                {
                    int newRowIndex = gvContentClears.Rows.Add();
                    DataGridViewRow row = gvContentClears.Rows[newRowIndex];
                    row.Cells[0].Value = c.Key;
                    row.Cells[1].Value = c.Value;
                }
            }
        }

        private void LoadAnalizerAddForm_Load(object sender, EventArgs e)
        {
            this.AnalizerForUseToUI();

            cgbExtraInfo.Collapse();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            #region 更新 parent.mAnalizerForUse
            parent.mAnalizerForUse = new AnalizerBase
             {
                 Name = txbName.Text.Trim(),
                 SiteName = txbSiteName.Text.Trim(),
                 SiteUrl = txbSiteUrl.Text.Trim(),
                 BookName = txbBookName.Text.Trim(),
                 Method = ddlMethod.SelectedIndex == 0 ? HttpMethod.Get : HttpMethod.Post,
                 Encoding = Encoding.GetEncoding(ddlEcoding.SelectedItem.ToString()),

                 BookNamePrefix = txbBookNamePrefix.Text.Trim(),
                 BooksUrl = txbBooksUrl.Text.Trim(),
                 BookRegex = txbBookRegex.Text.Trim(),
                 BookRegexTag = new string[] { "url", "title" },

                 ChaptersUrl = txbChaptersUrl.Text.Trim(),
                 ChapterRegex = txbChapterRegex.Text.Trim(),
                 ChapterRegexTag = new string[] { "url", "title" },

                 ContentBaseUrl = txbContentBaseUrl.Text.Trim(),
                 ContentRegex = txbContentRegex.Text.Trim(),
                 ContentClears = new Dictionary<string, string>()
             };

            foreach (DataGridViewRow row in gvContentClears.Rows)
            {
                if (row.Cells.Count == 0 || null == row.Cells[0].Value || string.IsNullOrWhiteSpace(row.Cells[0].Value.ToString())) continue;

                string key = row.Cells[0].Value.ToString();
                string value = (null == row.Cells[1].Value || string.IsNullOrWhiteSpace(row.Cells[1].Value.ToString())) ? null : row.Cells[1].Value.ToString();
                if (string.IsNullOrEmpty(key)) continue;

                if (!parent.mAnalizerForUse.ContentClears.ContainsKey(key))
                    parent.mAnalizerForUse.ContentClears.Add(key, value);
            } 
            #endregion

            #region 更新 parent.mAnalizers
            bool isUpdated = false;
            for (int i = 0; i < parent.mAnalizers.Count; i++)
            {
                if (parent.mAnalizers[i].Name == parent.mAnalizerForUse.Name)
                {
                    parent.mAnalizers[i] = parent.mAnalizerForUse.Clone();
                    isUpdated = true;
                    break;
                }
            }

            if (isUpdated == false)
            {
                parent.mAnalizers.Add(parent.mAnalizerForUse.Clone());
                parent.ddlAnalizerBind();
            }
            #endregion

            this.Close();
        }
    }
}
